#include "event.h"
#include "screen.h"
/*
    事件处理函数，处理按钮，编码器发送到队列的消息，
    根据消息内容进行实现相应的功能
*/

QueueHandle_t buttonEventQueue = NULL;
volatile bool need_screen_refresh = false;
int encoderValue = 0;

void init_events() {
    buttonEventQueue = xQueueCreate(20, sizeof(ButtonEvent));
    xTaskCreate(eventProcessingTask, "Event Task", 4096, NULL, 4, NULL);
}

void eventProcessingTask(void *pvParameters) {
    ButtonEvent evt;
    while (1) {
        if (xQueueReceive(buttonEventQueue, &evt, portMAX_DELAY) == pdPASS) {
            handleButtonEvent(&evt);
        }
    }
}

void handleButtonEvent(ButtonEvent *evt) {
    if (evt == NULL) return;

    switch (evt->type) {
    case EVENT_ENCODER_CLICK:
        need_screen_refresh = true;
        on_encoder_click_event();
        break;
    case EVENT_ENCODER_DOUBLE_CLICK:
        need_screen_refresh = true;
        on_encoder_double_click_event();
        break;
    case EVENT_ENCODER_ROTATE:
        handleEncoderRotation(evt->value);
        break;
    case EVENT_BUTTON_LONG_PRESS:
        // 长按处理逻辑
        break;
    }
}

void handleEncoderRotation(int value) {
    need_screen_refresh = true;
    lv_obj_invalidate(lv_scr_act());
}